package com.jet.rtdb.socket.client.mina;

import com.jet.rtdb.pojo.QueryHistoryDataList;
import com.jet.rtdb.pojo.QueryLastData;
import com.jet.rtdb.pojo.QueryLastDataList;
import org.apache.mina.core.future.ReadFuture;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.transport.socket.SocketSessionConfig;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetSocketAddress;
import java.util.concurrent.TimeUnit;

/**
 * @Author: zhangkaifeng.
 * @CreateTime: 2017/8/23 10:53
 * @Description:
 */


public class RtdbMinaClient {

    private IoSession session;

    private NioSocketConnector connector;

    private String ip;

    private int port;

    private static final Logger logger = LoggerFactory.getLogger(RtdbMinaClient.class);

    public RtdbMinaClient(String ip, int port) {
        this.ip = ip;
        this.port = port;
        getConnect();
    }

    public void getConnect() {
        connector = new NioSocketConnector();

        connector.setConnectTimeoutMillis(30000L);

        connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MyCodecFactory()));

        SocketSessionConfig cfg = connector.getSessionConfig();

        cfg.setUseReadOperation(true);

        session = connector.connect(new InetSocketAddress(ip, port)).awaitUninterruptibly().getSession();
    }

    public QueryLastDataList getLastData(QueryLastDataList queryLastDataList) {
        try {
            session.write(queryLastDataList).awaitUninterruptibly();
            ReadFuture readFuture = session.read();

            if (readFuture.awaitUninterruptibly(300, TimeUnit.SECONDS)) {
                Object msg = readFuture.getMessage();
                return (QueryLastDataList) msg;
            } else {
                logger.error("请求超时");
                return queryLastDataList;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.closeNow();
            connector.dispose();
        }
        return null;
    }

    public QueryHistoryDataList getHistoryData(QueryHistoryDataList queryHistoryDataList) {
        try {
            session.write(queryHistoryDataList).awaitUninterruptibly();
            ReadFuture readFuture = session.read();

            if (readFuture.awaitUninterruptibly(300, TimeUnit.SECONDS)) {
                Object msg = readFuture.getMessage();
                return (QueryHistoryDataList) msg;
            } else {
                logger.error("请求超时");
                return queryHistoryDataList;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.closeNow();
            connector.dispose();
        }
        return null;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 20; i++) {
            Long s = System.currentTimeMillis();
            RtdbMinaClient rtdbMinaClient = new RtdbMinaClient("192.168.1.206", 8090);
            QueryLastData queryLastData = new QueryLastData("w3.ryxbdt", "RYXBDT001_RYXBDT1-1-1_P");
            QueryLastDataList queryLastDatas = new QueryLastDataList();
            queryLastDatas.add(queryLastData);
            QueryLastDataList queryLastDatas1 = rtdbMinaClient.getLastData(queryLastDatas);
            System.out.println(queryLastDatas1+"耗时："+(System.currentTimeMillis() - s));
        }
    }

}
